#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import time
import gradio as gr
# from modules import txt2img, txt2video

#表情迁移的包：
from PaddleTools.GAN import FOM

#引入飞桨生态的语音和GAN依赖
from PaddleTools.TTS import TTSExecutor
from PaddleTools.GAN import wav2lip

#文本生成声音
def text2audio(txt2img_prompt):
    timestamp = str(int(time.time()))
    wav_name = "file/output/" + timestamp + ".wav"
    wav_path = os.path.join(os.path.join(os.path.dirname(__file__), wav_name))
    TTS = TTSExecutor('./default.yaml') #PaddleSpeech语音合成模块
    wavfile = TTS.run(text=txt2img_prompt,output= wav_path) #合成音频
    print('已成功创建虚拟人声音，文件保存在{}'.format(wav_path))


# 声音+图生成视频
def audio2img(audio2img_audio,audio2img_img):
    timestamp = str(int(time.time()))

    FOM_name = "file/output/" + timestamp + "_FOM.mp4"
    save_name = "file/output/" + timestamp + ".mp4"

    #原动作驱动视频
    DRIVING_VIDEO = os.path.join(os.path.join(os.path.dirname(__file__),"file/demo/xunizhubo_demo.mp4"))
    # print(DRIVING_VIDEO)

    #生成的动作迁移视频FOM
    FOM_path = os.path.join(os.path.join(os.path.dirname(__file__), FOM_name))
    # print(DRIVING_VIDEO)

    #目标视频保存地址
    save_path = os.path.join(os.path.join(os.path.dirname(__file__), save_name))

    #图片进行表情迁移
    FOM_FILE = FOM(audio2img_img,DRIVING_VIDEO,FOM_path)

    print('已成功创建虚拟人动作，文件保存在{}'.format(FOM_FILE))


    #将音频合成到唇形视频
    video = wav2lip(FOM_path,audio2img_audio,save_path) 

    print('视频生成完毕，输出路径为：{}'.format(save_path))
    return save_path



# 文+图生成视频
def txt2img(txt2img_prompt,txt2img_img):
    timestamp = str(int(time.time()))

    FOM_name = "file/output/" + timestamp + "_FOM.mp4"
    save_name = "file/output/" + timestamp + ".mp4"
    wav_name = "file/output/" + timestamp + ".wav"

    #原动作驱动视频
    DRIVING_VIDEO = os.path.join(os.path.join(os.path.dirname(__file__),"file/demo/xunizhubo_demo.mp4"))
    # print(DRIVING_VIDEO)

    #生成的动作迁移视频FOM
    FOM_path = os.path.join(os.path.join(os.path.dirname(__file__), FOM_name))
    # print(DRIVING_VIDEO)

    #目标视频保存地址
    save_path = os.path.join(os.path.join(os.path.dirname(__file__), save_name))
    wav_path = os.path.join(os.path.join(os.path.dirname(__file__), wav_name))


    #图片进行表情迁移
    FOM_FILE = FOM(txt2img_img,DRIVING_VIDEO,FOM_path)

    print('已成功创建虚拟人动作，文件保存在{}'.format(FOM_FILE))

    #文字生成声音，保存再临时声音文件 output.wav    
    TTS = TTSExecutor('./default.yaml') #PaddleSpeech语音合成模块
    wavfile = TTS.run(text=txt2img_prompt,output= wav_path) #合成音频
    print('已成功创建虚拟人声音，文件保存在{}'.format(wavfile))

    #将音频合成到唇形视频
    video = wav2lip(FOM_path,wav_path,save_path) 

    # os.remove('output.wav') #删除临时的音频文件

    print('视频生成完毕，输出路径为：{}'.format(save_path))
    return save_path

# 文+视频生成视频
def txt2video(txt2video_prompt, txt2video_video):
    timestamp = str(int(time.time()))
    save_name = "file/output/" + timestamp + ".mp4"
    wav_name = "file/output/" + timestamp + ".wav"

    #目标视频保存地址
    save_path = os.path.join(os.path.join(os.path.dirname(__file__), save_name))
    wav_path = os.path.join(os.path.join(os.path.dirname(__file__), wav_name))

    #文字生成声音，保存再临时声音文件 output.wav    
    TTS = TTSExecutor('./default.yaml') #PaddleSpeech语音合成模块
    wavfile = TTS.run(text=txt2img_prompt,output= wav_path) #合成音频
    print('已成功创建虚拟人声音，文件保存在{}'.format(wavfile))

    #将音频合成到唇形视频
    video = wav2lip(txt2video_video,wav_path,save_path) 

    # os.remove(wav_path) #删除临时的音频文件

    print('视频生成完毕，输出路径为：{}'.format(save_path))
    return save_path


if __name__ == '__main__':
    starttime = int(time.time())
    txt2img_prompt = "常记溪亭日暮，沉醉不知归路。兴尽晚回舟，误入藕花深处。争渡，争渡，惊起一滩鸥鹭。"
    txt2img_img = os.path.join(os.path.join(os.path.dirname(__file__),"file/demo/liqinzhao.jpg"))
    demo_video = os.path.join(os.path.join(os.path.dirname(__file__),"file/demo/xunizhubo_demo.mp4"))

    video_output = txt2img(txt2img_prompt,txt2img_img)
    # video2_output = txt2video(txt2img_prompt,demo_video)

    # text2audio(txt2img_prompt)

    print("消耗时间{}".format(int(time.time())-starttime))    


